package com.jxwifi.cloud.quickclean.mycenter;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.basicactivity.BasicActivity;
import com.idroid.utils.NoDoubleClickListener;
import com.jxwifi.cloud.quickclean.R;
import com.jxwifi.cloud.quickclean.app.AccountMgr;
import com.jxwifi.cloud.quickclean.app.ClientBasicActivity;
import com.jxwifi.cloud.quickclean.app.PasswordLogin;
import com.jxwifi.cloud.quickclean.bean.InfoBean;
import com.jxwifi.cloud.quickclean.utils.TitleClient;
import com.jxwifi.cloud.quickclean.utils.ToastUtils;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 修改昵称
 * Created by Administrator on 2019/6/19.
 */

public class SetNameActivity extends ClientBasicActivity implements SetNameView{


    String nameStr="\u0020\u0020";

    @Bind(R.id.et_set_name)
    EditText mEtSetName;
    private SetNamePresenter mPresenter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set_name);
        ButterKnife.bind(this);
        openEventBus();
        mPresenter=new SetNamePresenter(mBaseContext,this);
        initHead();
        initView();
    }

    private void initHead() {
        TitleClient holder = new TitleClient(ButterKnife.findById(this, R.id.lin_client_title));
        holder.defineTitle("更改昵称");
        holder.defineLeft(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                onBackPressed();
            }
        });
        holder.defineTextRight("保存", new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                if(AccountMgr.getName(mBaseContext)!=null){
                    if (!AccountMgr.getName(mBaseContext).equals(mEtSetName.getText().toString())){
                        mPresenter.profileName();
                    }else{
                        onBackPressed();
                    }
                }else{
                    if (mEtSetName.getText().toString()!=null){
                        mPresenter.profileName();
                    }else{
                        onBackPressed();
                    }
                }
            }
        });
    }

    public void initView(){
        mEtSetName.setText(AccountMgr.getName(mBaseContext));
    }

    @Override
    public EditText getSetName() {
        return mEtSetName;
    }

    @Override
    public void onprofileName() {
        PasswordLogin.passwordlogin(mBaseContext,TAG,AccountMgr.gettell(mBaseContext),AccountMgr.getpassword(mBaseContext));
        if (PasswordLogin.mIsLogin){
            mPresenter.profileName();
        }
    }

    @Override
    public void onSuccess(String data) {
        InfoBean mInfoBean= JSON.parseObject(data, InfoBean.class);
        AccountMgr.putUser(mBaseContext,mInfoBean,false);
        Toast toast = Toast.makeText(mBaseContext, "修改成功", Toast.LENGTH_SHORT);
        ToastUtils.showMyToast(toast, 3000);
        finish();
    }

    @Override
    public void onFailure(int errorCode, String msg) {
        Toast toast = Toast.makeText(mBaseContext, msg, Toast.LENGTH_SHORT);
        ToastUtils.showMyToast(toast, 3000);
    }
}
